setwd("~/Dropbox/GovResponsesCovid-19/Data")
library(haven);library(car);library(stargazer);library(cjoint);library(xtable)
d <-  read.csv("NelsonWitko_JBPA_ReplicationDataset.csv")
dim(d)
names(d)
summary(d$Duration..in.seconds.)
597.0/60
824.0/60
936.3/60
1131.8/60

d$pid <-NA
d$pid[d$PID2==1] <- 1
d$pid[d$PID2==2] <- 2
d$pid[d$PID4==2] <- 3
d$pid[d$PID4==3] <- 4
d$pid[d$PID4==1] <- 5
d$pid[d$PID3==2] <- 6
d$pid[d$PID3==1] <- 7
table(d$pid)

prop.table(table(d$GENDER))
prop.table(table(d$HISP))
d$YRBORN <- recode(d$YRBORN,"1880=1980")
d$YRBORN <- recode(d$YRBORN,"1881=1981")
summary(d$YRBORN)
d$age <- 2020-d$YRBORN
summary(d$age)
summary(d$IDEO)
prop.table(table(d$EDUC))
d$white <- grepl("1", d$RACE, fixed = TRUE)
726/984
d$black <- grepl("2", d$RACE, fixed = TRUE)
172/984

d$pid1 <- recode(d$pid,"1:3='D';4='I';5:7='R'")
d$concern1 <- recode(d$Q299,"1:2='Concern';3:4='Not';5='DK'")
chisq.test(table(d$pid1,d$concern1))
prop.table(table(d$pid1,d$concern1),2)

d$affected <- as.character(d$Q311)
d$affected <- recode(d$affected,"'7'=''")
d$affected <- gsub(",","",d$affected)
d$affected <- gsub("7","",d$affected)
d$affected <- nchar(d$affected)
d$cancelledTrip <- recode(d$Q315,"1=1;else=0")
d$affected <- d$affected + d$cancelledTrip
head(d$affected)
barplot(prop.table(table(d$affected)/sum(table(d$affected)))*100,xlab="Number of Actions Taken",ylab="Percentage of Respondents")
summary(d$affected)
quantile(d$affected, c(.33, .50, .67))

df <- setNames(data.frame(matrix(ncol = 15, nrow = dim(d)[1]*30)), c("responseId","respondent","trial","c_salary","c_size", "c_rural", "c_comp", "c_pol", "c_bs","c_rate","c_choose","pid","concern","worry","affected"))
d$ResponseId <- as.character(d$ResponseId)
respondent <- 1
row <- 1
for (respondent in 1:dim(d)[1]){
  df$responseId[row] <- d$ResponseId[respondent]
  df$responseId[row+1] <- d$ResponseId[respondent]
  df$responseId[row+2] <- d$ResponseId[respondent]
  df$responseId[row+3] <- d$ResponseId[respondent]
  df$responseId[row+4] <- d$ResponseId[respondent]
  df$responseId[row+5] <- d$ResponseId[respondent]
  df$responseId[row+6] <- d$ResponseId[respondent]
  df$responseId[row+7] <- d$ResponseId[respondent]
  df$responseId[row+8] <- d$ResponseId[respondent]
  df$responseId[row+9] <- d$ResponseId[respondent]
  df$responseId[row+10] <- d$ResponseId[respondent]
  df$responseId[row+11] <- d$ResponseId[respondent]
  df$responseId[row+12] <- d$ResponseId[respondent]
  df$responseId[row+13] <- d$ResponseId[respondent]
  df$responseId[row+14] <- d$ResponseId[respondent]
  df$responseId[row+15] <- d$ResponseId[respondent]
  df$responseId[row+16] <- d$ResponseId[respondent]
  df$responseId[row+17] <- d$ResponseId[respondent]
  df$responseId[row+18] <- d$ResponseId[respondent]
  df$responseId[row+19] <- d$ResponseId[respondent]
  df$responseId[row+20] <- d$ResponseId[respondent]
  df$responseId[row+21] <- d$ResponseId[respondent]
  df$responseId[row+22] <- d$ResponseId[respondent]
  df$responseId[row+23] <- d$ResponseId[respondent]
  df$responseId[row+24] <- d$ResponseId[respondent]
  df$responseId[row+25] <- d$ResponseId[respondent]
  df$responseId[row+26] <- d$ResponseId[respondent]
  df$responseId[row+27] <- d$ResponseId[respondent]
  df$responseId[row+28] <- d$ResponseId[respondent]
  df$responseId[row+29] <- d$ResponseId[respondent]
  df$respondent[row] <- respondent
  df$respondent[row+1] <- respondent
  df$respondent[row+2] <- respondent
  df$respondent[row+3] <- respondent
  df$respondent[row+4] <- respondent
  df$respondent[row+5] <- respondent
  df$respondent[row+6] <- respondent
  df$respondent[row+7] <- respondent
  df$respondent[row+8] <- respondent
  df$respondent[row+9] <- respondent
  df$respondent[row+10] <- respondent
  df$respondent[row+11] <- respondent
  df$respondent[row+12] <- respondent
  df$respondent[row+13] <- respondent
  df$respondent[row+14] <- respondent
  df$respondent[row+15] <- respondent
  df$respondent[row+16] <- respondent
  df$respondent[row+17] <- respondent
  df$respondent[row+18] <- respondent
  df$respondent[row+19] <- respondent
  df$respondent[row+20] <- respondent
  df$respondent[row+21] <- respondent
  df$respondent[row+22] <- respondent
  df$respondent[row+23] <- respondent
  df$respondent[row+24] <- respondent
  df$respondent[row+25] <- respondent
  df$respondent[row+26] <- respondent
  df$respondent[row+27] <- respondent
  df$respondent[row+28] <- respondent
  df$respondent[row+29] <- respondent
  df$trial[row] <- 1
  df$trial[row+1] <- 1
  df$trial[row+2] <- 2
  df$trial[row+3] <- 2
  df$trial[row+4] <- 3
  df$trial[row+5] <- 3
  df$trial[row+6] <- 4
  df$trial[row+7] <- 4
  df$trial[row+8] <- 5
  df$trial[row+9] <- 5
  df$trial[row+10] <- 6
  df$trial[row+11] <- 6
  df$trial[row+12] <- 7
  df$trial[row+13] <- 7
  df$trial[row+14] <- 8
  df$trial[row+15] <- 8
  df$trial[row+16] <- 9
  df$trial[row+17] <- 9
  df$trial[row+18] <- 10
  df$trial[row+19] <- 10
  df$trial[row+20] <- 11
  df$trial[row+21] <- 11
  df$trial[row+22] <- 12
  df$trial[row+23] <- 12
  df$trial[row+24] <- 13
  df$trial[row+25] <- 13
  df$trial[row+26] <- 14
  df$trial[row+27] <- 14
  df$trial[row+28] <- 15
  df$trial[row+29] <- 15
  df$c_number[row] <- 1
  df$c_number[row+1] <- 2
  df$c_number[row+2] <- 1
  df$c_number[row+3] <- 2
  df$c_number[row+4] <- 1
  df$c_number[row+5] <- 2
  df$c_number[row+6] <- 1
  df$c_number[row+7] <- 2
  df$c_number[row+8] <- 1
  df$c_number[row+9] <- 2
  df$c_number[row+10] <- 1
  df$c_number[row+11] <- 2
  df$c_number[row+12] <- 1
  df$c_number[row+13] <- 2
  df$c_number[row+14] <- 1
  df$c_number[row+15] <- 2
  df$c_number[row+16] <- 1
  df$c_number[row+17] <- 2
  df$c_number[row+18] <- 1
  df$c_number[row+19] <- 2
  df$c_number[row+20] <- 1
  df$c_number[row+21] <- 2
  df$c_number[row+22] <- 1
  df$c_number[row+23] <- 2
  df$c_number[row+24] <- 1
  df$c_number[row+25] <- 2
  df$c_number[row+26] <- 1
  df$c_number[row+27] <- 2
  df$c_number[row+28] <- 1
  df$c_number[row+29] <- 2

  df$pid[row] <- d$pid[respondent]
  df$pid[row+1] <- d$pid[respondent]
  df$pid[row+2] <- d$pid[respondent]
  df$pid[row+3] <- d$pid[respondent]
  df$pid[row+4] <- d$pid[respondent]
  df$pid[row+5] <- d$pid[respondent]
  df$pid[row+6] <- d$pid[respondent]
  df$pid[row+7] <- d$pid[respondent]
  df$pid[row+8] <- d$pid[respondent]
  df$pid[row+9] <- d$pid[respondent]
  df$pid[row+10] <- d$pid[respondent]
  df$pid[row+11] <- d$pid[respondent]
  df$pid[row+12] <- d$pid[respondent]
  df$pid[row+13] <- d$pid[respondent]
  df$pid[row+14] <- d$pid[respondent]
  df$pid[row+15] <- d$pid[respondent]
  df$pid[row+16] <- d$pid[respondent]
  df$pid[row+17] <- d$pid[respondent]
  df$pid[row+18] <- d$pid[respondent]
  df$pid[row+19] <- d$pid[respondent]
  df$pid[row+20] <- d$pid[respondent]
  df$pid[row+21] <- d$pid[respondent]
  df$pid[row+22] <- d$pid[respondent]
  df$pid[row+23] <- d$pid[respondent]
  df$pid[row+24] <- d$pid[respondent]
  df$pid[row+25] <- d$pid[respondent]
  df$pid[row+26] <- d$pid[respondent]
  df$pid[row+27] <- d$pid[respondent]
  df$pid[row+28] <- d$pid[respondent]
  df$pid[row+29] <- d$pid[respondent]
  df$concern[row] <- d$Q299[respondent]
  df$concern[row+1] <- d$Q299[respondent]
  df$concern[row+2] <- d$Q299[respondent]
  df$concern[row+3] <- d$Q299[respondent]
  df$concern[row+4] <- d$Q299[respondent]
  df$concern[row+5] <- d$Q299[respondent]
  df$concern[row+6] <- d$Q299[respondent]
  df$concern[row+7] <- d$Q299[respondent]
  df$concern[row+8] <- d$Q299[respondent]
  df$concern[row+9] <- d$Q299[respondent]
  df$concern[row+10] <- d$Q299[respondent]
  df$concern[row+11] <- d$Q299[respondent]
  df$concern[row+12] <- d$Q299[respondent]
  df$concern[row+13] <- d$Q299[respondent]
  df$concern[row+14] <- d$Q299[respondent]
  df$concern[row+15] <- d$Q299[respondent]
  df$concern[row+16] <- d$Q299[respondent]
  df$concern[row+17] <- d$Q299[respondent]
  df$concern[row+18] <- d$Q299[respondent]
  df$concern[row+19] <- d$Q299[respondent]
  df$concern[row+20] <- d$Q299[respondent]
  df$concern[row+21] <- d$Q299[respondent]
  df$concern[row+22] <- d$Q299[respondent]
  df$concern[row+23] <- d$Q299[respondent]
  df$concern[row+24] <- d$Q299[respondent]
  df$concern[row+25] <- d$Q299[respondent]
  df$concern[row+26] <- d$Q299[respondent]
  df$concern[row+27] <- d$Q299[respondent]
  df$concern[row+28] <- d$Q299[respondent]
  df$concern[row+29] <- d$Q299[respondent]
  df$worry[row] <- d$Q301[respondent]
  df$worry[row+1] <- d$Q301[respondent]
  df$worry[row+2] <- d$Q301[respondent]
  df$worry[row+3] <- d$Q301[respondent]
  df$worry[row+4] <- d$Q301[respondent]
  df$worry[row+5] <- d$Q301[respondent]
  df$worry[row+6] <- d$Q301[respondent]
  df$worry[row+7] <- d$Q301[respondent]
  df$worry[row+8] <- d$Q301[respondent]
  df$worry[row+9] <- d$Q301[respondent]
  df$worry[row+10] <- d$Q301[respondent]
  df$worry[row+11] <- d$Q301[respondent]
  df$worry[row+12] <- d$Q301[respondent]
  df$worry[row+13] <- d$Q301[respondent]
  df$worry[row+14] <- d$Q301[respondent]
  df$worry[row+15] <- d$Q301[respondent]
  df$worry[row+16] <- d$Q301[respondent]
  df$worry[row+17] <- d$Q301[respondent]
  df$worry[row+18] <- d$Q301[respondent]
  df$worry[row+19] <- d$Q301[respondent]
  df$worry[row+20] <- d$Q301[respondent]
  df$worry[row+21] <- d$Q301[respondent]
  df$worry[row+22] <- d$Q301[respondent]
  df$worry[row+23] <- d$Q301[respondent]
  df$worry[row+24] <- d$Q301[respondent]
  df$worry[row+25] <- d$Q301[respondent]
  df$worry[row+26] <- d$Q301[respondent]
  df$worry[row+27] <- d$Q301[respondent]
  df$worry[row+28] <- d$Q301[respondent]
  df$worry[row+29] <- d$Q301[respondent]
  df$affected[row] <- d$affected[respondent]
  df$affected[row+1] <- d$affected[respondent]
  df$affected[row+2] <- d$affected[respondent]
  df$affected[row+3] <- d$affected[respondent]
  df$affected[row+4] <- d$affected[respondent]
  df$affected[row+5] <- d$affected[respondent]
  df$affected[row+6] <- d$affected[respondent]
  df$affected[row+7] <- d$affected[respondent]
  df$affected[row+8] <- d$affected[respondent]
  df$affected[row+9] <- d$affected[respondent]
  df$affected[row+10] <- d$affected[respondent]
  df$affected[row+11] <- d$affected[respondent]
  df$affected[row+12] <- d$affected[respondent]
  df$affected[row+13] <- d$affected[respondent]
  df$affected[row+14] <- d$affected[respondent]
  df$affected[row+15] <- d$affected[respondent]
  df$affected[row+16] <- d$affected[respondent]
  df$affected[row+17] <- d$affected[respondent]
  df$affected[row+18] <- d$affected[respondent]
  df$affected[row+19] <- d$affected[respondent]
  df$affected[row+20] <- d$affected[respondent]
  df$affected[row+21] <- d$affected[respondent]
  df$affected[row+22] <- d$affected[respondent]
  df$affected[row+23] <- d$affected[respondent]
  df$affected[row+24] <- d$affected[respondent]
  df$affected[row+25] <- d$affected[respondent]
  df$affected[row+26] <- d$affected[respondent]
  df$affected[row+27] <- d$affected[respondent]
  df$affected[row+28] <- d$affected[respondent]
  df$affected[row+29] <- d$affected[respondent]
  #Trial 1
  df$c_salary[row] <- d$sal1_DO[respondent]
  df$c_size[row] <- d$size1_DO[respondent]
  df$c_rural[row] <- d$rural1_DO[respondent]
  df$c_comp[row] <- d$comp1_DO[respondent]
  df$c_pol[row] <- d$pol1_DO[respondent]
  df$c_bs[row] <- d$bs1_DO[respondent]
  df$c_rate[row] <- d$offer1_1[respondent]
  df$c_choose[row] <- d$accept_1[respondent]
  
  df$c_salary[row+1] <- d$sal2_DO[respondent]
  df$c_size[row+1] <- d$size2_DO[respondent]
  df$c_rural[row+1] <- d$rural2_DO[respondent]
  df$c_comp[row+1] <- d$comp2_DO[respondent]
  df$c_pol[row+1] <- d$pol2_DO[respondent]
  df$c_bs[row+1] <- d$bs2_DO[respondent]
  df$c_rate[row+1] <- d$offer2_1[respondent]
  df$c_choose[row+1] <- 3-d$accept_1[respondent]  
  
  #Trial 2
  df$c_salary[row+2] <- d$sal3_DO[respondent]
  df$c_size[row+2] <- d$size3_DO[respondent]
  df$c_rural[row+2] <- d$rural3_DO[respondent]
  df$c_comp[row+2] <- d$comp3_DO[respondent]
  df$c_pol[row+2] <- d$pol3_DO[respondent]
  df$c_bs[row+2] <- d$bs3_DO[respondent]
  df$c_rate[row+2] <- d$offer1_2[respondent]
  df$c_choose[row+2] <- d$accept_2[respondent]
  
  df$c_salary[row+3] <- d$sal4_DO[respondent]
  df$c_size[row+3] <- d$size4_DO[respondent]
  df$c_rural[row+3] <- d$rural4_DO[respondent]
  df$c_comp[row+3] <- d$comp4_DO[respondent]
  df$c_pol[row+3] <- d$pol4_DO[respondent]
  df$c_bs[row+3] <- d$bs4_DO[respondent]
  df$c_rate[row+3] <- d$offer2_2[respondent]
  df$c_choose[row+3] <- 3-d$accept_2[respondent]  

  #Trial 3
  df$c_salary[row+4] <- d$sal5_DO[respondent]
  df$c_size[row+4] <- d$size5_DO[respondent]
  df$c_rural[row+4] <- d$rural5_DO[respondent]
  df$c_comp[row+4] <- d$comp5_DO[respondent]
  df$c_pol[row+4] <- d$pol5_DO[respondent]
  df$c_bs[row+4] <- d$bs5_DO[respondent]
  df$c_rate[row+4] <- d$offer1_3[respondent]
  df$c_choose[row+4] <- d$accept_3[respondent]
  
  df$c_salary[row+5] <- d$sal6_DO[respondent]
  df$c_size[row+5] <- d$size6_DO[respondent]
  df$c_rural[row+5] <- d$rural6_DO[respondent]
  df$c_comp[row+5] <- d$comp6_DO[respondent]
  df$c_pol[row+5] <- d$pol6_DO[respondent]
  df$c_bs[row+5] <- d$bs6_DO[respondent]
  df$c_rate[row+5] <- d$offer2_3[respondent]
  df$c_choose[row+5] <- 3-d$accept_3[respondent]  
  
  #Trial 4
  df$c_salary[row+6] <- d$sal7_DO[respondent]
  df$c_size[row+6] <- d$size7_DO[respondent]
  df$c_rural[row+6] <- d$rural7_DO[respondent]
  df$c_comp[row+6] <- d$comp7_DO[respondent]
  df$c_pol[row+6] <- d$pol7_DO[respondent]
  df$c_bs[row+6] <- d$bs7_DO[respondent]
  df$c_rate[row+6] <- d$offer1_4[respondent]
  df$c_choose[row+6] <- d$accept_4[respondent]
  
  df$c_salary[row+7] <- d$sal8_DO[respondent]
  df$c_size[row+7] <- d$size8_DO[respondent]
  df$c_rural[row+7] <- d$rural8_DO[respondent]
  df$c_comp[row+7] <- d$comp8_DO[respondent]
  df$c_pol[row+7] <- d$pol8_DO[respondent]
  df$c_bs[row+7] <- d$bs8_DO[respondent]
  df$c_rate[row+7] <- d$offer2_4[respondent]
  df$c_choose[row+7] <- 3-d$accept_4[respondent]  
  
  #Trial 5
  df$c_salary[row+8] <- d$sal9_DO[respondent]
  df$c_size[row+8] <- d$size9_DO[respondent]
  df$c_rural[row+8] <- d$rural9_DO[respondent]
  df$c_comp[row+8] <- d$comp9_DO[respondent]
  df$c_pol[row+8] <- d$pol9_DO[respondent]
  df$c_bs[row+8] <- d$bs9_DO[respondent]
  df$c_rate[row+8] <- d$offer1_5[respondent]
  df$c_choose[row+8] <- d$accept_9[respondent]
  
  df$c_salary[row+9] <- d$sal10_DO[respondent]
  df$c_size[row+9] <- d$size10_DO[respondent]
  df$c_rural[row+9] <- d$rural10_DO[respondent]
  df$c_comp[row+9] <- d$comp10_DO[respondent]
  df$c_pol[row+9] <- d$pol10_DO[respondent]
  df$c_bs[row+9] <- d$bs10_DO[respondent]
  df$c_rate[row+9] <- d$offer2_5[respondent]
  df$c_choose[row+9] <- 3-d$accept_5[respondent]  
  
  #Trial 6
  df$c_salary[row+10] <- d$sal11_DO[respondent]
  df$c_size[row+10] <- d$size11_DO[respondent]
  df$c_rural[row+10] <- d$rural11_DO[respondent]
  df$c_comp[row+10] <- d$comp11_DO[respondent]
  df$c_pol[row+10] <- d$pol11_DO[respondent]
  df$c_bs[row+10] <- d$bs11_DO[respondent]
  df$c_rate[row+10] <- d$offer1_6[respondent]
  df$c_choose[row+10] <- d$accept_6[respondent]
  
  df$c_salary[row+11] <- d$sal12_DO[respondent]
  df$c_size[row+11] <- d$size12_DO[respondent]
  df$c_rural[row+11] <- d$rural12_DO[respondent]
  df$c_comp[row+11] <- d$comp12_DO[respondent]
  df$c_pol[row+11] <- d$pol12_DO[respondent]
  df$c_bs[row+11] <- d$bs12_DO[respondent]
  df$c_rate[row+11] <- d$offer2_6[respondent]
  df$c_choose[row+11] <- 3-d$accept_6[respondent]  
  
  #Trial 7
  df$c_salary[row+12] <- d$sal13_DO[respondent]
  df$c_size[row+12] <- d$size13_DO[respondent]
  df$c_rural[row+12] <- d$rural13_DO[respondent]
  df$c_comp[row+12] <- d$comp13_DO[respondent]
  df$c_pol[row+12] <- d$pol13_DO[respondent]
  df$c_bs[row+12] <- d$bs13_DO[respondent]
  df$c_rate[row+12] <- d$offer1_7[respondent]
  df$c_choose[row+12] <- d$accept_7[respondent]
  
  df$c_salary[row+13] <- d$sal14_DO[respondent]
  df$c_size[row+13] <- d$size14_DO[respondent]
  df$c_rural[row+13] <- d$rural14_DO[respondent]
  df$c_comp[row+13] <- d$comp14_DO[respondent]
  df$c_pol[row+13] <- d$pol14_DO[respondent]
  df$c_bs[row+13] <- d$bs14_DO[respondent]
  df$c_rate[row+13] <- d$offer2_7[respondent]
  df$c_choose[row+13] <- 3-d$accept_7[respondent]  
  
  #Trial 8
  df$c_salary[row+14] <- d$sal15_DO[respondent]
  df$c_size[row+14] <- d$size15_DO[respondent]
  df$c_rural[row+14] <- d$rural15_DO[respondent]
  df$c_comp[row+14] <- d$comp15_DO[respondent]
  df$c_pol[row+14] <- d$pol15_DO[respondent]
  df$c_bs[row+14] <- d$bs15_DO[respondent]
  df$c_rate[row+14] <- d$offer1_8[respondent]
  df$c_choose[row+14] <- d$accept_8[respondent]
  
  df$c_salary[row+15] <- d$sal16_DO[respondent]
  df$c_size[row+15] <- d$size16_DO[respondent]
  df$c_rural[row+15] <- d$rural16_DO[respondent]
  df$c_comp[row+15] <- d$comp16_DO[respondent]
  df$c_pol[row+15] <- d$pol16_DO[respondent]
  df$c_bs[row+15] <- d$bs16_DO[respondent]
  df$c_rate[row+15] <- d$offer2_8[respondent]
  df$c_choose[row+15] <- 3-d$accept_8[respondent]  
  
  #Trial 9
  df$c_salary[row+16] <- d$sal17_DO[respondent]
  df$c_size[row+16] <- d$size17_DO[respondent]
  df$c_rural[row+16] <- d$rural17_DO[respondent]
  df$c_comp[row+16] <- d$comp17_DO[respondent]
  df$c_pol[row+16] <- d$pol17_DO[respondent]
  df$c_bs[row+16] <- d$bs17_DO[respondent]
  df$c_rate[row+16] <- d$offer1_9[respondent]
  df$c_choose[row+16] <- d$accept_9[respondent]
  
  df$c_salary[row+17] <- d$sal18_DO[respondent]
  df$c_size[row+17] <- d$size18_DO[respondent]
  df$c_rural[row+17] <- d$rural18_DO[respondent]
  df$c_comp[row+17] <- d$comp18_DO[respondent]
  df$c_pol[row+17] <- d$pol18_DO[respondent]
  df$c_bs[row+17] <- d$bs18_DO[respondent]
  df$c_rate[row+17] <- d$offer2_9[respondent]
  df$c_choose[row+17] <- 3-d$accept_9[respondent]  
  
  #Trial 10
  df$c_salary[row+18] <- d$sal19_DO[respondent]
  df$c_size[row+18] <- d$size19_DO[respondent]
  df$c_rural[row+18] <- d$rural19_DO[respondent]
  df$c_comp[row+18] <- d$comp19_DO[respondent]
  df$c_pol[row+18] <- d$pol19_DO[respondent]
  df$c_bs[row+18] <- d$bs19_DO[respondent]
  df$c_rate[row+18] <- d$offer1_10[respondent]
  df$c_choose[row+18] <- d$accept_10[respondent]
  
  df$c_salary[row+19] <- d$sal20_DO[respondent]
  df$c_size[row+19] <- d$size20_DO[respondent]
  df$c_rural[row+19] <- d$rural20_DO[respondent]
  df$c_comp[row+19] <- d$comp20_DO[respondent]
  df$c_pol[row+19] <- d$pol20_DO[respondent]
  df$c_bs[row+19] <- d$bs20_DO[respondent]
  df$c_rate[row+19] <- d$offer2_10[respondent]
  df$c_choose[row+19] <- 3-d$accept_10[respondent]  

  #Trial 11
  df$c_salary[row+20] <- d$sal21_DO[respondent]
  df$c_size[row+20] <- d$size21_DO[respondent]
  df$c_rural[row+20] <- d$rural21_DO[respondent]
  df$c_comp[row+20] <- d$comp21_DO[respondent]
  df$c_pol[row+20] <- d$pol21_DO[respondent]
  df$c_bs[row+20] <- d$bs21_DO[respondent]
  df$c_rate[row+20] <- d$offer1_11[respondent]
  df$c_choose[row+20] <- d$accept_11[respondent]
  
  df$c_salary[row+21] <- d$sal22_DO[respondent]
  df$c_size[row+21] <- d$size22_DO[respondent]
  df$c_rural[row+21] <- d$rural22_DO[respondent]
  df$c_comp[row+21] <- d$comp22_DO[respondent]
  df$c_pol[row+21] <- d$pol2_DO[respondent]
  df$c_bs[row+21] <- d$bs22_DO[respondent]
  df$c_rate[row+21] <- d$offer2_11[respondent]
  df$c_choose[row+21] <- 3-d$accept_11[respondent]    

  #Trial 12
  df$c_salary[row+22] <- d$sal23_DO[respondent]
  df$c_size[row+22] <- d$size23_DO[respondent]
  df$c_rural[row+22] <- d$rural23_DO[respondent]
  df$c_comp[row+22] <- d$comp23_DO[respondent]
  df$c_pol[row+22] <- d$pol23_DO[respondent]
  df$c_bs[row+22] <- d$bs23_DO[respondent]
  df$c_rate[row+22] <- d$offer1_12[respondent]
  df$c_choose[row+22] <- d$accept_12[respondent]
  
  df$c_salary[row+23] <- d$sal24_DO[respondent]
  df$c_size[row+23] <- d$size24_DO[respondent]
  df$c_rural[row+23] <- d$rural24_DO[respondent]
  df$c_comp[row+23] <- d$comp24_DO[respondent]
  df$c_pol[row+23] <- d$pol24_DO[respondent]
  df$c_bs[row+23] <- d$bs24_DO[respondent]
  df$c_rate[row+23] <- d$offer2_12[respondent]
  df$c_choose[row+23] <- 3-d$accept_12[respondent]  
  
  #Trial 13
  df$c_salary[row+24] <- d$sal25_DO[respondent]
  df$c_size[row+24] <- d$size25_DO[respondent]
  df$c_rural[row+24] <- d$rural25_DO[respondent]
  df$c_comp[row+24] <- d$comp25_DO[respondent]
  df$c_pol[row+24] <- d$pol25_DO[respondent]
  df$c_bs[row+24] <- d$bs25_DO[respondent]
  df$c_rate[row+24] <- d$offer1_13[respondent]
  df$c_choose[row+24] <- d$accept_13[respondent]
  
  df$c_salary[row+25] <- d$sal26_DO[respondent]
  df$c_size[row+25] <- d$size26_DO[respondent]
  df$c_rural[row+25] <- d$rural26_DO[respondent]
  df$c_comp[row+25] <- d$comp26_DO[respondent]
  df$c_pol[row+25] <- d$pol26_DO[respondent]
  df$c_bs[row+25] <- d$bs26_DO[respondent]
  df$c_rate[row+25] <- d$offer2_13[respondent]
  df$c_choose[row+25] <- 3-d$accept_13[respondent]  
  
  #Trial 14
  df$c_salary[row+26] <- d$sal27_DO[respondent]
  df$c_size[row+26] <- d$size27_DO[respondent]
  df$c_rural[row+26] <- d$rural27_DO[respondent]
  df$c_comp[row+26] <- d$comp27_DO[respondent]
  df$c_pol[row+26] <- d$pol27_DO[respondent]
  df$c_bs[row+26] <- d$bs27_DO[respondent]
  df$c_rate[row+26] <- d$offer1_14[respondent]
  df$c_choose[row+26] <- d$accept_14[respondent]
  
  df$c_salary[row+27] <- d$sal28_DO[respondent]
  df$c_size[row+27] <- d$size28_DO[respondent]
  df$c_rural[row+27] <- d$rural28_DO[respondent]
  df$c_comp[row+27] <- d$comp28_DO[respondent]
  df$c_pol[row+27] <- d$pol28_DO[respondent]
  df$c_bs[row+27] <- d$bs28_DO[respondent]
  df$c_rate[row+27] <- d$offer2_14[respondent]
  df$c_choose[row+27] <- 3-d$accept_14[respondent]  
  
  #Trial 15
  df$c_salary[row+28] <- d$sal29_DO[respondent]
  df$c_size[row+28] <- d$size29_DO[respondent]
  df$c_rural[row+28] <- d$rural29_DO[respondent]
  df$c_comp[row+28] <- d$comp29_DO[respondent]
  df$c_pol[row+28] <- d$pol29_DO[respondent]
  df$c_bs[row+28] <- d$bs29_DO[respondent]
  df$c_rate[row+28] <- d$offer1_15[respondent]
  df$c_choose[row+28] <- d$accept_15[respondent]
  
  df$c_salary[row+29] <- d$sal30_DO[respondent]
  df$c_size[row+29] <- d$size30_DO[respondent]
  df$c_rural[row+29] <- d$rural30_DO[respondent]
  df$c_comp[row+29] <- d$comp30_DO[respondent]
  df$c_pol[row+29] <- d$pol30_DO[respondent]
  df$c_bs[row+29] <- d$bs30_DO[respondent]
  df$c_rate[row+29] <- d$offer2_15[respondent]
  df$c_choose[row+29] <- 3-d$accept_5[respondent]  
  
  row <- row+30
}

df$c_choose <- recode(df$c_choose,"2=0")
df$c_salary <- factor(recode(df$c_salary,"1='1_75k';2='2_90k';3='3-105k'"))
df$c_size <- factor(recode(df$c_size,"1='1_10Employees';2='2-2500Employees';3='3_500000Employees'"))
df$c_rural <- factor(recode(df$c_rural,"1='Rural';2='CollegeTown';3='Midsize';4='Metro'"))
df$c_comp <- factor(recode(df$c_comp,"1='Variety';2='Feedback';3='Advancement';4='Talent'"))
df$c_pol <- factor(recode(df$c_pol,"1='1_ClintonStrong';2='2_ClintonWeak';3='3_TrumpWeak';4='4_TrumpStrong'"))
df$c_bs <- factor(recode(df$c_bs,"1='Proclamation';2='Delegate';3='LimitGatherings';4='StayAtHome'"))
df$c_rate <- recode(df$c_rate,"1=5;2=4;5=3;3=2;4=1")

table(df$trial)
table(df$c_salary)
table(df$c_size)
table(df$c_rural)
table(df$c_comp)
table(df$c_pol)
table(df$c_bs)
prop.table(table(df$c_rate))
prop.table(table(df$c_choose))

df$Salary <- factor(df$c_salary,levels=c("1_75k", "2_90k", "3-105k"),labels=c("$75,000","$90,000","$105,000"))
df$Size <- factor(df$c_size,levels=c("1_10Employees", "2-2500Employees", "3_500000Employees"),labels=c("10 Employees","2,500 Employees","500,000 Employees"))
df$Location <- factor(df$c_rural,levels=c("CollegeTown","Metro","Midsize","Rural"),labels=c("College Town","Metro Area","Midsize City","Rural Area"))
df$Culture <- factor(df$c_comp,levels=c("Advancement","Feedback", "Talent","Variety"),labels=c("Advancement Opportunity","Frequent Feedback", "Great Talent","Task Variety"))
df$Partisanship <- factor(df$c_pol,levels=c("1_ClintonStrong","2_ClintonWeak","3_TrumpWeak","4_TrumpStrong"),labels=c("Strong Clinton State","Weak Clinton State","Weak Trump State","Strong Trump State"))
df$COVID <- factor(df$c_bs,levels=c("Proclamation","Delegate","LimitGatherings","StayAtHome"),labels=c("Proclamation","Delegate","Limit Gatherings","Stay at Home"))
df$Party <- recode(df$pid, "1:3=0;4=1;5:7=2")
df$Party <- factor(df$Party,levels=c(0,1,2),labels=c("Democrat","Independent","Republican"))
df$Concerned <- recode(df$concern,"1:2=1;3:5=0")
df$Concerned <- factor(df$Concerned,levels=c(0,1),labels=c("Not Concerned","Concerned"))

df1 <- na.omit(df)
mturk <- df1

##Basic Results
results <- amce(c_choose~Salary+Size+Location+Culture+Partisanship+COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent")
summary(results)
restab <- cbind(summary(results)$amce[1],summary(results)$amce[2],round(summary(results)$amce[3],2),round(summary(results)$amce[4],2),round(summary(results)$amce[6],2))
colnames(restab) <- c("Attribute","Level","AMCE","Std. Err.","P-Value")
print(xtable(restab,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResults.pdf",width=8,height=6)
plot(results, xlab="Change in Pr(Choose Job)",
     ylim=c(-.3,.3), breaks=c(-.2, 0, .2,.4), labels=c("-.2","0",".2",".4"), text.size=13)
dev.off()
results$estimates

results <- amce(c_rate~Salary+Size+Location+Culture+Partisanship+COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent")
summary(results)
restab <- cbind(summary(results)$amce[1],summary(results)$amce[2],round(summary(results)$amce[3],2),round(summary(results)$amce[4],2),round(summary(results)$amce[6],2))
colnames(restab) <- c("Attribute","Level","AMCE","Std. Err.","P-Value")
print(xtable(restab,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResultsRating.pdf",width=8,height=6)
plot(results, xlab="Change in Job Rating",
     ylim=c(-.5,.7), breaks=c(-.4,-.2, 0, .2,.4,.6), labels=c("-.4","-.2","0",".2",".4",".6"), text.size=13)
dev.off()
results$estimates

##By Party
results <- amce(c_choose~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
restabinteract <- cbind(summary(results)$Party1amce[1],summary(results)$Party1amce[2],round(summary(results)$Party1amce[3],2),round(summary(results)$Party1amce[4],2),round(summary(results)$Party1amce[6],2),round(summary(results)$Party2amce[3],2),round(summary(results)$Party2amce[4],2),round(summary(results)$Party2amce[6],2),round(summary(results)$Party3amce[3],2),round(summary(results)$Party3amce[4],2),round(summary(results)$Party3amce[6],2))
colnames(restabinteract) <- c("Attribute","Level","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value")
print(xtable(restabinteract,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResultsParty.pdf",width=12,height=12)
plot(results, xlab="Change in Pr(Choose Job)",
     ylim=c(-.3,.3), breaks=c(-.2, 0, .2,.4), labels=c("-.2","0",".2",".4"), text.size=13,
     plot.display="interaction",group.order=c("COVID","Culture","Location","Partisanship","Salary","Size"))
dev.off()
results$estimates

results <- amce(c_rate~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
restabinteract <- cbind(summary(results)$Party1amce[1],summary(results)$Party1amce[2],round(summary(results)$Party1amce[3],2),round(summary(results)$Party1amce[4],2),round(summary(results)$Party1amce[6],2),round(summary(results)$Party2amce[3],2),round(summary(results)$Party2amce[4],2),round(summary(results)$Party2amce[6],2),round(summary(results)$Party3amce[3],2),round(summary(results)$Party3amce[4],2),round(summary(results)$Party3amce[6],2))
colnames(restabinteract) <- c("Attribute","Level","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value")
print(xtable(restabinteract,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResultsPartyRating.pdf",width=12,height=12)
plot(results, xlab="Change in Job Rating",
     ylim=c(-.5,.7), breaks=c(-1,-.8,-.6,-.4,-.2, 0, .2,.4,.6,.8), labels=c("-1","-.8","-.6","-.4","-.2","0",".2",".4",".6",".8"), text.size=13,
     plot.display="interaction",group.order=c("COVID","Culture","Location","Partisanship","Salary","Size"))
dev.off()
results$estimates

##By Concern
results <- amce(c_choose~Concerned*Salary+Concerned*Size+Concerned*Location+Concerned*Culture+Concerned*Partisanship+Concerned*COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Concerned")
summary(results)
restabinteract <- cbind(summary(results)$Concerned1amce[1],summary(results)$Concerned1amce[2],round(summary(results)$Concerned1amce[3],2),round(summary(results)$Concerned1amce[4],2),round(summary(results)$Concerned1amce[6],2),round(summary(results)$Concerned2amce[3],2),round(summary(results)$Concerned2amce[4],2),round(summary(results)$Concerned2amce[6],2))
colnames(restabinteract) <- c("Attribute","Level","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value")
print(xtable(restabinteract,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResultsConcerned.pdf",width=12,height=12)
plot(results, xlab="Change in Pr(Choose Job)",
     ylim=c(-.3,.3), breaks=c(-.2, 0, .2,.4), labels=c("-.2","0",".2",".4"), text.size=13,
     plot.display="interaction",group.order=c("COVID","Culture","Location","Partisanship","Salary","Size"))
dev.off()
results$estimates

results <- amce(c_rate~Concerned*Salary+Concerned*Size+Concerned*Location+Concerned*Culture+Concerned*Partisanship+Concerned*COVID, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Concerned")
summary(results)
restabinteract <- cbind(summary(results)$Concerned1amce[1],summary(results)$Concerned1amce[2],round(summary(results)$Concerned1amce[3],2),round(summary(results)$Concerned1amce[4],2),round(summary(results)$Concerned1amce[6],2),round(summary(results)$Concerned2amce[3],2),round(summary(results)$Concerned2amce[4],2),round(summary(results)$Concerned2amce[6],2))
colnames(restabinteract) <- c("Attribute","Level","AMCE","Std. Err.","P-Value","AMCE","Std. Err.","P-Value")
print(xtable(restabinteract,caption="Estimated AMCEs"), include.rownames=FALSE)

pdf("basicResultsConcernedRating.pdf",width=12,height=12)
plot(results, xlab="Change in Job Rating",
     ylim=c(-.5,.7), breaks=c(-1,-.8,-.6,-.4,-.2, 0, .2,.4,.6,.8), labels=c("-1","-.8","-.6","-.4","-.2","0",".2",".4",".6",".8"), text.size=13,
     plot.display="interaction",group.order=c("COVID","Culture","Location","Partisanship","Salary","Size"))
dev.off()
results$estimates

##Marginal Means
library(cregg)

cj_anova(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+COVID, by = ~Party)
cj_anova(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+COVID, by = ~Concerned)
cj_anova(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+COVID, by = ~trial)

##By Party
mturk$Party1 <- factor(mturk$Party,levels=c("Republican","Independent","Democrat"),ordered=T)
x <- cj(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+COVID, by = ~Party, id = ~respondent, estimate = "mm")
plot(x, vline = 0.5, group="Party") + ggplot2::facet_wrap(~Party, nrow = 1,drop=TRUE) 

print(xtable(cbind(as.character(x[x$BY=="Democrat","feature"]),as.character(x[x$BY=="Democrat","level"]),format(round(x[x$BY=="Democrat","estimate"],2), nsmall = 2),format(round(x[x$BY=="Democrat","std.error"],2), nsmall = 2),format(round(x[x$BY=="Democrat","p"],2), nsmall = 2),
                   format(round(x[x$BY=="Independent","estimate"],2), nsmall = 2),format(round(x[x$BY=="Independent","std.error"],2), nsmall = 2),format(round(x[x$BY=="Independent","p"],2), nsmall = 2),
                   format(round(x[x$BY=="Republican","estimate"],2), nsmall = 2),format(round(x[x$BY=="Republican","std.error"],2), nsmall = 2),format(round(x[x$BY=="Republican","p"],2), nsmall = 2))), include.rownames=FALSE)

##By Concern
x <- cj(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+COVID, by = ~Concerned, id = ~respondent, estimate = "mm")
plot(x, group = "Concerned", vline = 0.5)
+ ggplot2::facet_wrap(~Concerned, nrow = 1) 
print(xtable(cbind(as.character(x[x$BY=="Concerned","feature"]),as.character(x[x$BY=="Concerned","level"]),format(round(x[x$BY=="Concerned","estimate"],2), nsmall = 2),format(round(x[x$BY=="Concerned","std.error"],2), nsmall = 2),format(round(x[x$BY=="Concerned","p"],2), nsmall = 2),
                   format(round(x[x$BY=="Not Concerned","estimate"],2), nsmall = 2),format(round(x[x$BY=="Not Concerned","std.error"],2), nsmall = 2),format(round(x[x$BY=="Not Concerned","p"],2), nsmall = 2))), include.rownames=FALSE)
